﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

namespace DreamBox.BLL.Admin
{
    public class GetGroupCompetenceTree
    {
        //循环选择下面的节点
        public void TreeNodeCheck(TreeNode node)
        {
            foreach (TreeNode child in node.ChildNodes)
            {
                child.Checked = node.Checked;
                TreeNodeCheck(child);
            }
        }
        //获取获取权限树
        public void GetTreeList(DataTable dt,DataTable gDt,TreeView tree)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row["ParentID"].ToString() == "0")
                {
                    TreeNode root = new TreeNode();
                    root.Text = row["CompetenceName"].ToString();
                    root.Value = row["ComID"].ToString();
                    root.ShowCheckBox = true;
                    root.Checked = GetChecked(gDt, row["ComID"].ToString());
                    tree.Nodes.Add(root);
                    GetSubTreeNode(dt,gDt,root,row["ComID"].ToString());
                }
            }
        }
        //获取子节点
        private void GetSubTreeNode(DataTable dt,DataTable gDt,TreeNode parentNode,string parentID)
        {
            foreach (DataRow row in dt.Rows)
            {
                if (row["ParentID"].ToString() == parentID)
                {
                    TreeNode child = new TreeNode();
                    child.Text = row["CompetenceName"].ToString();
                    child.Value = row["ComID"].ToString();
                    child.ShowCheckBox = true;
                    child.Checked= GetChecked(gDt, row["ComID"].ToString());
                    parentNode.ChildNodes.Add(child);
                    GetSubTreeNode(dt,gDt, child, row["ComID"].ToString());
                }
            }
        }
       //判断是否存在数据
        private bool GetChecked(DataTable gDt,string ComID)
        {
            DataRow[] rows = gDt.Select("ComID=" + ComID);
            if (rows.Length > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
